import axios from 'axios'
import store from '@/store'
import { Message, MessageBox } from 'element-ui'
import { getToken } from '@/utils/auth'

// const baseUrRL = (process.env.NODE_ENV === 'development') ? '/' : 'http://10.3.64.79:8090/'
// create an axios instance
const service = axios.create({
  // baseURL: 'http://127.0.0.1:9020/', // 测试网关
  baseURL: '', // 本地开发不走网关
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 300000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    if (store.getters.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      // config.headers['Authorization'] = getToken()
      config.headers['Authorization'] = getToken()
      // config.headers['X-Token'] = getToken()
    }
    if (config.method === 'get' || config.method === 'delete') {
      config.data = true
    }
    config.headers['Content-Type'] = 'application/json'
    config.headers['Accept'] = 'application/json'
    return config
  },
  error => {
    // do something with request error
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */
  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  // response => response,
  // response => {
  //   const res = response.data
  //   console.log(res.returnCode)
  //   // if the custom code is not 20000, it is judged as an error.
  //   if (res.returnCode !== '00200') {
  //     Message({
  //       message: res.message || 'Error',
  //       type: 'error',
  //       duration: 5 * 1000
  //     })
  //
  //     // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  //     /* if (res.returnCode === 50008 || res.returnCode === 50012 || res.returnCode === 50014) {
  //       // to re-login
  //       MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
  //         confirmButtonText: 'Re-Login',
  //         cancelButtonText: 'Cancel',
  //         type: 'warning'
  //       }).then(() => {
  //         store.dispatch('user/resetToken').then(() => {
  //           location.reload()
  //         })
  //       })
  //     } */
  //     if (res.returnCode === '00620') {
  //       store.dispatch('user/refreshToken').then(() => {
  //         location.reload()
  //       })
  //     }
  //   } else {
  //     return res
  //   }
  // },
  response => {
    if (response.status === 500) {
      return Promise.reject(new Error('服务异常，请稍后重试！'))
    }
    const res = response.data
    // if the custom code is not 20000, it is judged as an error.
    if (res.returnCode !== '00200' & res.returnCode !== '00900') {
      Message({
        message: res.msg || 'Error',
        type: 'error',
        duration: 5 * 1000
      })

      /* // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
      if (res.returnCode === 50008 || res.returnCode === 50012 || res.returnCode === 50014) {
        // to re-login
        MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
          confirmButtonText: 'Re-Login',
          cancelButtonText: 'Cancel',
          type: 'warning'
        }).then(() => {
          store.dispatch('user/resetToken').then(() => {
            location.reload()
          })
        })
      }*/
      return Promise.reject(new Error(res.msg || 'Error'))
    } else {
      return res
    }
  },
  error => {
    if (error.response.status === 401 && error.response.data.returnCode !== '00603') {
      MessageBox.alert('登录信息失效，请重新登录', {}).then(() => {
        store.dispatch('user/resetToken').then(() => {
          location.reload()
        })
      })
    } else {
      Message({
        message: error.response.data.msg,
        type: 'error',
        duration: 5 * 1000
      })
      const message = (error.response && error.response.data && error.response.data.msg) || '请求失败，请稍后重试'
      return Promise.reject(message)
    }
  }
)
export default service
